package com.yz.lzq.liwushuo.ui.modules.category.danping;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.jsonbean.category.CategroyDanPBean;
import com.yz.lzq.liwushuo.ui.modules.category.gongl.CategoryListViewAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by AdminJia on 2016/11/28.
 */

public class CategoryDanPFragment extends Fragment implements ICategoryDanPView {
    @Bind(R.id.recyclerView_category_danP)
    RecyclerView recyclerViewCategoryDanP;
    @Bind(R.id.listView_category_danP)
    ListView listViewCategoryDanP;
    private Context mContext = null;
    private CategoryDanPPresenter categoryDanPPresenter = null;
    private int listViewItemClickId = 1;
    private CategoryDanPAdapter adapter1 = null;
    private CategoryListViewAdapter adapter2 = null;
    private GridLayoutManager manager = null;
    private int firstVisibleItemPosition = 0;
    private int listViewsetSelectionPosintion = 0;
    private int itemVisibleNum = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        Fresco.initialize(mContext);
        categoryDanPPresenter = new CategoryDanPPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category_danp, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        categoryDanPPresenter.loadData(mContext);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void setAdapterSuccess(CategoryDanPAdapter _adapter, GridLayoutManager _manager) {
        this.manager = _manager;
        this.adapter1 = _adapter;
        recyclerViewCategoryDanP.setLayoutManager(manager);
        recyclerViewCategoryDanP.setAdapter(adapter1);

        Log.i("TAG", "----------setAdapterSuccess: " + adapter1.getItemCount());
        recyclerViewCategoryDanP.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                // firstVisibleItemPosition = manager.findFirstVisibleItemPosition();


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
                if (firstVisibleItemPosition >= adapter1.getItemCount()) {
                    return;
                }
                switch (adapter1.getItemViewType(firstVisibleItemPosition)) {
                    case 0:
                        CategroyDanPBean.DataBean.CategoriesBean o = (CategroyDanPBean.DataBean
                                .CategoriesBean) adapter1.getmList_object().get
                                (firstVisibleItemPosition);
                        // Toast.makeText(mContext, "" + o.getName(), Toast.LENGTH_SHORT).show();
                        listViewsetSelectionPosintion = o.getId();
                        for (int i = 0; i < adapter2.getCount(); i++) {
                            if (adapter2.getCategories().get(i).getId() ==
                                    listViewsetSelectionPosintion) {
                                Log.i("TAG", "------>>>>>>>>>onScrollStateChanged: " +
                                        listViewsetSelectionPosintion);
                                listViewCategoryDanP.setItemChecked(i, true);
                                if (i <= itemVisibleNum) {

                                    listViewCategoryDanP.setSelection(0);
                                } else if (i > itemVisibleNum && i <= adapter2.getCount()) {
                                    listViewCategoryDanP.setSelection(i - 2);
                                } else {
                                    listViewCategoryDanP.setSelection(adapter2.getCount() -
                                            itemVisibleNum + 1);
                                }
                                return;
                            }
                        }

                        break;

                }
            }
        });
    }

    @Override
    public void setListViewAdapterSuccess(final CategoryListViewAdapter _adapter) {
        this.adapter2 = _adapter;
        AnimationSet set = new AnimationSet(false);
        Animation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1, Animation
                .RELATIVE_TO_SELF, 0);
        set.addAnimation(translateAnimation);
        //Starting alpha value for the animation, where 1.0 means fully opaque and 0.0
        // means fully transparent.
//				ScaleAnimation scaleAnimation = new ScaleAnimation(0.8f, 1.2f, 0.8f, 1.2f);
//				set.addAnimation(scaleAnimation);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 1.0f);
        set.addAnimation(alphaAnimation);
        set.setDuration(1800);
        set.setFillAfter(true);
        listViewCategoryDanP.setAnimation(set);
        listViewCategoryDanP.setAdapter(adapter2);
        itemVisibleNum = listViewCategoryDanP.getFirstVisiblePosition() - listViewCategoryDanP
                .getLastVisiblePosition() + 1;
        listViewCategoryDanP.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        listViewCategoryDanP.setItemChecked(0, true);

        Log.i("TAG", "-------<<<<<<<<<setAdapterSuccess: adapter2 " + adapter2.getCount());
        listViewCategoryDanP.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                listViewItemClickId = adapter2.getCategories().get(i).getId();
                // Log.i("TAG", "-----------------onItemClick: " + listViewItemClickId);
                Toast.makeText(mContext, "o.getName()" + listViewCategoryDanP, Toast
                        .LENGTH_SHORT).show();
                for (int j = 0; j < adapter1.getmList_object().size(); j++) {
                    int itemViewType = adapter1.getItemViewType(j);
                    switch (itemViewType) {
                        case 0:
                            CategroyDanPBean.DataBean.CategoriesBean o = (CategroyDanPBean
                                    .DataBean.CategoriesBean) adapter1.getmList_object().get(j);
                            if (listViewItemClickId == o.getId()) {
                                //    Log.i("TAG", "-----------------onItemClick: " + o.getName());
                                manager.scrollToPositionWithOffset(j, 0);

                            }
                    }

                }
            }
        });

    }


    @Override
    public void setAdapterFailed(Throwable t) {
        t.printStackTrace();

    }
}
